﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MailSortSettingRepositoryTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MailSortSettingRepositoryTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EFRepositoryTests
{
  using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data.Entity;
  using System.Linq;
  using System.Transactions;
  using BuildMeA;
  using DocProcessingRepository.Repositories;
  using Entities;
  using FluentAssertions;
  using NUnit.Framework;

  [Category("Integration")]
  [TestFixture]
  public class MailSortSettingRepositoryTests
  {
    private TransactionScope transactionScope;
    private MailSortSettingRepository mailSortSettingRepository;

    private MailsortSetting testMailSortSetting;

    [SetUp]
    public void Setup()
    {
      this.transactionScope = new TransactionScope();
      String connectionString = ConfigurationManager.ConnectionStrings["BBOS"].ConnectionString;
      this.mailSortSettingRepository = new MailSortSettingRepository(connectionString);

      this.testMailSortSetting = BuildMeA.MailSortSetting("test", "2", "1000", "test", "test", true, 50);
    }

    [TearDown]
    public void TearDown()
    {
      this.transactionScope.Dispose();
    }

    [Test]
    public void GivenNothing_WhenIGetMailSortSettings_TheyAreRetrievedFromTheDatabase()
    {
      this.mailSortSettingRepository.Create(this.testMailSortSetting);

      List<MailsortSetting> allMailSortSettings = this.mailSortSettingRepository.GetMailsortSettings();

      allMailSortSettings.Should()
        .Contain(
          m =>
            m.TNTDepot == "test" &&
            m.TNTMailingHouse == "2" &&
            m.TNTClientCode == "1000" &&
            m.TNTJobReference == "test" &&
            m.nexdoxMailsortService == "test" &&
            m.largeletter.Value &&
            m.FixedWeight_NoOuter == 50);
    }

    [Test]
    public void GivenValidData_WhenIAddAMailSortSetting_ItIsAddedToTheDatabase()
    {
      Int32 existingCount = this.mailSortSettingRepository.Entities.Count();
      Int32 maxSequence = this.mailSortSettingRepository.Entities.Max(m => m.MailsortSettingID);

      this.mailSortSettingRepository.AddMailSortSetting("TNT Depot", "2", "1000", "TNT Job Reference", "Nexdox Mailsort Service", true, 52);

      this.mailSortSettingRepository.Entities.Count().Should().Be(existingCount + 1);

      List<MailsortSetting> mailSorts =
        this.mailSortSettingRepository.Entities.Where(m => m.MailsortSettingID > maxSequence).ToList();

      mailSorts.Should().HaveCount(1);
      MailsortSetting mailSort = mailSorts.Single();

      mailSort.TNTDepot.Should().Be("TNT Depot");
      mailSort.TNTMailingHouse.Should().Be("2");
      mailSort.TNTClientCode.Should().Be("1000");
      mailSort.TNTJobReference.Should().Be("TNT Job Reference");
      mailSort.nexdoxMailsortService.Should().Be("Nexdox Mailsort Service");
      mailSort.largeletter.Should().BeTrue();
      mailSort.FixedWeight_NoOuter.Should().Be(52);
    }

    [Test]
    public void GivenValidDataWithSomeNulls_WhenIAddAMailSortSetting_ItIsAddedToTheDatabase()
    {
      Int32 existingCount = this.mailSortSettingRepository.Entities.Count();
      Int32 maxSequence = this.mailSortSettingRepository.Entities.Max(m => m.MailsortSettingID);

      this.mailSortSettingRepository.AddMailSortSetting(null, null, null, null, null, false, null);

      this.mailSortSettingRepository.Entities.Count().Should().Be(existingCount + 1);

      List<MailsortSetting> mailSorts =
        this.mailSortSettingRepository.Entities.Where(m => m.MailsortSettingID > maxSequence).ToList();

      mailSorts.Should().HaveCount(1);
      MailsortSetting mailSort = mailSorts.Single();

      mailSort.TNTDepot.Should().Be(null);
      mailSort.TNTMailingHouse.Should().Be(null);
      mailSort.TNTClientCode.Should().Be(null);
      mailSort.TNTJobReference.Should().Be(null);
      mailSort.nexdoxMailsortService.Should().Be(null);
      mailSort.largeletter.Should().BeFalse();
      mailSort.FixedWeight_NoOuter.Should().Be(null);
    }
  }
}
